﻿using Mathmall.Administration.Authorization;
using Mathmall.Administration.Data;
using Mathmall.Administration.Helper;
using Mathmall.Administration.Mapper;
using Mathmall.Administration.Models;
using Mathmall.Administration.Service;
using Mathmall.Administration.Service.impl;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;

namespace Mathmall.Administration.Config
{
    public static class MyConfigServiceCollectionExtensions
    {
        /// <summary>
        /// 添加配置的扩展方法
        /// </summary>
        /// <param name="services"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static IServiceCollection AddConfig(
            this IServiceCollection services, IConfiguration config)
        {
            services.Configure<UploadOptions>(
                config.GetSection(UploadOptions.Upload));

            return services;
        }

        /// <summary>
        /// 服务注册依赖项
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddMyDependencyGroup(
            this IServiceCollection services)
        {
            services.AddScoped<IUserService, UserService>();
            services.AddScoped<UserMapper>();
            services.AddScoped<IBrandService, BrandService>();
            services.AddScoped<BrandMapper>();
            services.AddScoped<ICategoryService, CategoryService>();
            services.AddScoped<CategoryMapper>();
            services.AddScoped<IAttrService, AttrService>();
            services.AddScoped<AttrMapper>();
            services.AddScoped<IRoleService, RoleService>();
            services.AddScoped<RoleMapper>();
            services.AddScoped<JwtHelper>();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped<IAttrGroupService, AttrGroupService>();
            services.AddScoped<AttrGroupMapper>();
            services.AddScoped<IMenuService, MenuService>();
            services.AddScoped<MenuMapper>();
            services.AddScoped<IRoleMenuService, RoleMenuService>();
            services.AddScoped<RoleMenuMapper>();
            services.AddScoped<ICategoryBrandRelationService, CategoryBrandRelationService>();
            services.AddScoped<CategoryBrandRelationMapper>();
            services.AddScoped<IAttrAttrGroupRelationService, AttrAttrGroupRelationService>();
            services.AddScoped<AttrAttrGroupRelationMapper>();

            services.AddAuthorization(options =>
            {
                options.AddPolicy("QueryBrand",
                    policy => policy.Requirements.Add(new OperationRequirement("QueryBrand")));
                options.AddPolicy("AddBrand",
                    policy => policy.Requirements.Add(new OperationRequirement("AddBrand")));
                options.AddPolicy("EditBrand",
                    policy => policy.Requirements.Add(new OperationRequirement("EditBrand")));
                options.AddPolicy("DeleteBrand",
                    policy => policy.Requirements.Add(new OperationRequirement("DeleteBrand")));

                options.AddPolicy("QueryCategory",
                    policy => policy.Requirements.Add(new OperationRequirement("QueryCategory")));
                options.AddPolicy("AddCategory",
                    policy => policy.Requirements.Add(new OperationRequirement("AddCategory")));
                options.AddPolicy("EditCategory",
                    policy => policy.Requirements.Add(new OperationRequirement("EditCategory")));
                options.AddPolicy("DeleteCategory",
                    policy => policy.Requirements.Add(new OperationRequirement("DeleteCategory")));

                options.AddPolicy("QueryUser",
                    policy => policy.Requirements.Add(new OperationRequirement("QueryUser")));
                options.AddPolicy("AddUser",
                    policy => policy.Requirements.Add(new OperationRequirement("AddUser")));
                options.AddPolicy("EditUser",
                    policy => policy.Requirements.Add(new OperationRequirement("EditUser")));
                options.AddPolicy("DeleteUser",
                    policy => policy.Requirements.Add(new OperationRequirement("DeleteUser")));
                options.AddPolicy("AssignRole",
                    policy => policy.Requirements.Add(new OperationRequirement("AssignRole")));

                options.AddPolicy("QueryRole",
                    policy => policy.Requirements.Add(new OperationRequirement("QueryRole")));
                options.AddPolicy("AddRole",
                    policy => policy.Requirements.Add(new OperationRequirement("AddRole")));
                options.AddPolicy("EditRole",
                    policy => policy.Requirements.Add(new OperationRequirement("EditRole")));
                options.AddPolicy("DeleteRole",
                    policy => policy.Requirements.Add(new OperationRequirement("DeleteRole")));
                options.AddPolicy("AssignPermission",
                    policy => policy.Requirements.Add(new OperationRequirement("AssignPermission")));

                options.AddPolicy("QueryMenu",
                    policy => policy.Requirements.Add(new OperationRequirement("QueryMenu")));
                options.AddPolicy("AddMenu",
                    policy => policy.Requirements.Add(new OperationRequirement("AddMenu")));
                options.AddPolicy("EditMenu",
                    policy => policy.Requirements.Add(new OperationRequirement("EditMenu")));
                options.AddPolicy("DeleteMenu",
                    policy => policy.Requirements.Add(new OperationRequirement("DeleteMenu")));
            });
            return services;
        }
    }
}